﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using ColorApplication.Helper;
using System.Configuration;

namespace ColorApplication.color
{
    public partial class AttributeXAttributefamilies : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Checking whether Admin or Not. AdminID key is in web.config
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                Clear();
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
            try
            {
                // If ColorId is Pass Set value for Data Source of grid view
                if (Request.QueryString["AttributeFamiliesId"] != null)
                {
                    if (Request.QueryString["AttributeFamiliesId"].ToString() != string.Empty)
                    {
                        int AttributeFamiliesId = Convert.ToInt32(Request.QueryString["AttributeFamiliesId"].ToString());
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Clear();

                        Parameter ControlIDparemeter = new Parameter("Id");
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(ControlIDparemeter);

                        Parameter AttributeFamiliesColorparameter = new Parameter("AttributeFamiliesId", DbType.Int32, AttributeFamiliesId.ToString());
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(AttributeFamiliesColorparameter);

                        Parameter Attributeparameter = new Parameter("AttributeId");
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(Attributeparameter);

                    }
                }
                else if (Request.QueryString["AttributeId"] != null)
                {
                    if (Request.QueryString["AttributeId"].ToString() != string.Empty)
                    {
                        int AttributeId = Convert.ToInt32(Request.QueryString["AttributeId"].ToString());
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Clear();

                        Parameter ControlIDparemeter = new Parameter("Id");
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(ControlIDparemeter);

                        Parameter AttributeFamiliesColorparameter = new Parameter("AttributeFamiliesId");
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(AttributeFamiliesColorparameter);

                        Parameter Attributeparameter = new Parameter("AttributeId", DbType.Int32, AttributeId.ToString());
                        AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(Attributeparameter);

                    }
                }
                else
                {
                    // If Next Time remove ColorId value for Data Source of grid view

                    AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Clear();

                    Parameter ControlIDparemeter = new Parameter("Id");
                    AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(ControlIDparemeter);

                    Parameter AttributeFamiliesColorparameter = new Parameter("AttributeFamiliesId");
                    AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(AttributeFamiliesColorparameter);

                    Parameter Attributeparameter = new Parameter("AttributeId");
                    AttributesXAttributesfamiliesGridSqlDataSource.SelectParameters.Add(Attributeparameter);


                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();

            }


        }

        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            SuccesLabel.Text = "";
            ErrorLabel.Text = "";

        }

        protected void AttributesXAttributesfamiliesGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // Change Deatils view Mode to edit
                AttributesXAttributesfamiliesDetailsView.ChangeMode(DetailsViewMode.Edit);
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributesXAttributesfamiliesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            try
            {
                // Bind java Script for Are you Sure to Delete Confirm Box
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton delinkbutton = (LinkButton)e.Row.FindControl("DeleteLinkButton");
                    delinkbutton.OnClientClick = "return getDeleteMessage()";

                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributesXAttributesfamiliesDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {

            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record updated successfully.";
                AttributesXAttributesfamiliesDetailsView.ChangeMode(DetailsViewMode.Insert);
                AttributesXAttributesfamiliesGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributesXAttributesfamiliesDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {

            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record inserted successfully.";
                AttributesXAttributesfamiliesDetailsView.ChangeMode(DetailsViewMode.Insert);
                AttributesXAttributesfamiliesGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributesXAttributesfamiliesDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                int OldAttributeId = Convert.ToInt32(e.OldValues["AttributeId"].ToString());
                int OldAttributesfamiliesId = Convert.ToInt32(e.OldValues["AttributesfamiliesId"].ToString());

                int NewAttributeId = Convert.ToInt32(e.NewValues["AttributeId"].ToString());
                int NewAttributesfamiliesId = Convert.ToInt32(e.NewValues["AttributesfamiliesId"].ToString());

                if (OldAttributeId != NewAttributeId || OldAttributesfamiliesId != NewAttributesfamiliesId)
                {
                    //  This Function Check Record in Attribute X AttributeFamily for Dublicate record exist or Not
                    if (CheckAttributesXAttributesfamiliesExist(NewAttributeId, NewAttributesfamiliesId) == true)
                    {
                        ErrorLabel.Text = "Attribute X Attribute Families already exists. Duplicate record is not allowed.";
                        e.Cancel = true;
                        //Cancel And Show Error Message
                    }

                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void AttributesXAttributesfamiliesDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            try
            {
                int AttributeId = Convert.ToInt32(e.Values["AttributeId"].ToString());
                int AttributesfamiliesId = Convert.ToInt32(e.Values["AttributesfamiliesId"].ToString());
                //  This Function Check Record in Attribute X AttributeFamily for Dublicate record exist or Not
                if (CheckAttributesXAttributesfamiliesExist(AttributeId, AttributesfamiliesId) == true)
                {
                    ErrorLabel.Text = "Attribute X Attribute Families already exists. Duplicate record is not allowed.";
                    e.Cancel = true;
                    //Cancel And Show Error Message
                }


            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        public bool CheckAttributesXAttributesfamiliesExist(int AttributeId, int AttributesfamiliesId)
        {
            // This Function Check Record in  ColorAttribute Table For Dublicate record Exist or Not
            // if Record Found Return true
            //else return false

            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters = { new SqlParameter("@AttributeId",AttributeId),
                                              new SqlParameter("@AttributesfamiliesId",AttributesfamiliesId) 
                                            };

                SqlCommand cmd = SQLHelper.GetCommand(con, "CheckAttributesXAttributesfamiliesExist", parameters);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                return true;
            }

            return false;

        }

        protected void InsertAttributeDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["AttributeId"] != null)
                {
                    if (Request.QueryString["AttributeId"].ToString() != string.Empty)
                    {
                        int AttributeId = Convert.ToInt32(Request.QueryString["AttributeId"].ToString());
                        DropDownList InsertAttributeDropDownList = (DropDownList)AttributesXAttributesfamiliesDetailsView.FindControl("InsertAttributeDropDownList");
                        if (InsertAttributeDropDownList != null)
                        {
                            InsertAttributeDropDownList.SelectedValue = AttributeId.ToString();
                        }


                    }

                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }


        }

        protected void InsertAttributeFamiliesDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["AttributeFamiliesId"] != null)
                {
                    if (Request.QueryString["AttributeFamiliesId"].ToString() != string.Empty)
                    {
                        int AttributeFamiliesId = Convert.ToInt32(Request.QueryString["AttributeFamiliesId"].ToString());
                        DropDownList InsertAttributeFamiliesDropDownList = (DropDownList)AttributesXAttributesfamiliesDetailsView.FindControl("InsertAttributeFamiliesDropDownList");
                        if (InsertAttributeFamiliesDropDownList != null)
                        {
                            InsertAttributeFamiliesDropDownList.SelectedValue = AttributeFamiliesId.ToString();
                        }
                    }
                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }

        }
    }
}